package com.cloudsigns.factorymanager2.project.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.activity.AddEngineeringFeedBackActivity;
import com.cloudsigns.factorymanager2.engineering.activity.EngineeringInstallDailyFeedBackDetailActivity;
import com.cloudsigns.factorymanager2.engineering.activity.EngineeringInstallFeedBackReplyForTeamActivity;
import com.cloudsigns.factorymanager2.engineering.adapter.DailyFeedBackForEngineeringInstallAdapter;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringDailyFeedBackBean;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringInstallationOrderBean;
import com.cloudsigns.factorymanager2.engineering.fragment.DailyFeedBackForEngineeringInstallFragment;
import com.cloudsigns.factorymanager2.project.activity.SurveyAddBaseFeedBackActivity;
import com.cloudsigns.factorymanager2.project.activity.SurveyAddPrepareWorkFeedBackActivity;
import com.cloudsigns.factorymanager2.project.activity.SurveyAddSizeFeedBackActivity;
import com.cloudsigns.factorymanager2.project.activity.SurveyAddSizeFeedBackActivity1;
import com.cloudsigns.factorymanager2.project.activity.SurveyFeedBackForSizeDetailsActivity;
import com.cloudsigns.factorymanager2.project.adapter.SurveyFeedBackForBaseInfoAdapter;
import com.cloudsigns.factorymanager2.project.adapter.SurveyFeedBackForSizeAdapter;
import com.cloudsigns.factorymanager2.project.adapter.SurveyFeedBackForSizeAdapter1;
import com.cloudsigns.factorymanager2.project.bean.SurVeyOrderBean;
import com.cloudsigns.factorymanager2.project.bean.SurveyFeedBackForBaseInfoBean;
import com.cloudsigns.factorymanager2.project.bean.SurveyFeedBackForSizeBean;
import com.cloudsigns.factorymanager2.project.bean.SurveyFeedbackContentBean;
import com.cloudsigns.factorymanager2.project.iview.ISurveyFeedbackAddView;
import com.cloudsigns.factorymanager2.project.myinerface.ItemClickListener;
import com.cloudsigns.factorymanager2.project.presenter.SurveyFeedbackAddPresenter;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by tony on 2018/8/5.
 */

public class SurveyAddFeedBackForTeamFragment extends Fragment implements ISurveyFeedbackAddView, View.OnClickListener {
    private View rl_size, rl_baseInfo, rl_prepare;
    private ListView lv_size, lv_baseInfo, lv_prepare;
    private ImageView img_add_feedback_size, img_add_feedback_baseInfo, img_add_feedback_prepare;
    private TextView btn_submit;
    private ScrollView scrollview;
    //private SurveyFeedBackForSizeAdapter sizeAdapter;
    private SurveyFeedBackForSizeAdapter1 sizeAdapter;
    private SurveyFeedBackForBaseInfoAdapter baseinfoAdapter;
    private SurveyFeedBackForBaseInfoAdapter prepareAdapter;
    private List<SurveyFeedBackForSizeBean> sizeDataList = new ArrayList<>();
    private List<SurveyFeedBackForBaseInfoBean> baseinfoDataList = new ArrayList<>();
    private List<SurveyFeedBackForBaseInfoBean> prepareDataList = new ArrayList<>();
    private String TAG = SurveyAddFeedBackForTeamFragment.class.getSimpleName();
    private SurVeyOrderBean surVeyOrderBean;
    private boolean isClick = true;
    private SurveyFeedbackAddPresenter surveyFeedbackAddPresenter;

    private CommonEnum.SurveySizeType surveySizeType;
    private String content;
    private int width;
    private int hight;
    private int corner;
    private int hightDif;
    private int specialWidth;
    private int specialHight;
    private int arcLength;
    private int arcHight;
    private List<String> imgUrlList;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        surveyFeedbackAddPresenter = new SurveyFeedbackAddPresenter(this);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_survey_add_feedback_for_team, null);
        rl_size = view.findViewById(R.id.rl_size);
        rl_baseInfo = view.findViewById(R.id.rl_baseInfo);
        rl_prepare = view.findViewById(R.id.rl_prepare);
        lv_size = view.findViewById(R.id.lv_size);
        lv_baseInfo = view.findViewById(R.id.lv_baseInfo);
        lv_prepare = view.findViewById(R.id.lv_prepare);
        btn_submit = view.findViewById(R.id.btn_submit);
        img_add_feedback_size = view.findViewById(R.id.img_add_feedback_size);
        img_add_feedback_baseInfo = view.findViewById(R.id.img_add_feedback_baseInfo);
        img_add_feedback_prepare = view.findViewById(R.id.img_add_feedback_prepare);

        sizeAdapter = new SurveyFeedBackForSizeAdapter1(getActivity(), TAG);
        sizeAdapter.setData(sizeDataList);
        lv_size.setAdapter(sizeAdapter);

        baseinfoAdapter = new SurveyFeedBackForBaseInfoAdapter(getActivity(), TAG);
        baseinfoAdapter.setData(baseinfoDataList);
        lv_baseInfo.setAdapter(baseinfoAdapter);

        prepareAdapter = new SurveyFeedBackForBaseInfoAdapter(getActivity(), TAG);
        prepareAdapter.setData(prepareDataList);
        lv_prepare.setAdapter(prepareAdapter);


        btn_submit.setOnClickListener(this);
        img_add_feedback_size.setOnClickListener(this);
        img_add_feedback_baseInfo.setOnClickListener(this);
        img_add_feedback_prepare.setOnClickListener(this);

        lv_size.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View view, final int arg2, long arg3) {
                Object object = lv_size.getItemAtPosition(arg2);
                if (object instanceof SurveyFeedBackForSizeBean) {
                    SurveyFeedBackForSizeBean info = (SurveyFeedBackForSizeBean) object;
                    surveySizeType = info.getSurveySizeType();
                    content = info.getContent();
                    width = info.getWidth();
                    hight = info.getHight();
                    corner = info.getCorner();
                    hightDif = info.getHightDif();
                    specialWidth = info.getSpecialWidth();
                    specialHight = info.getSpecialHight();
                    arcLength = info.getArcLength();
                    arcHight = info.getArcHight();
                    imgUrlList = info.getImgUrlList();
                }
                Intent intent = new Intent(getActivity(), SurveyFeedBackForSizeDetailsActivity.class);
                intent.putExtra("surveySizeType", surveySizeType);
                intent.putExtra("content", content);
                intent.putExtra("width", width);
                intent.putExtra("hight", hight);
                intent.putExtra("corner", corner);
                intent.putExtra("hightDif", hightDif);
                intent.putExtra("specialWidth", specialWidth);
                intent.putExtra("specialHight", specialHight);
                intent.putExtra("arcLength", arcLength);
                intent.putExtra("arcHight", arcHight);
                intent.putStringArrayListExtra("imgUrlList", (ArrayList<String>) imgUrlList);
                startActivity(intent);
            }
        });
        return view;
    }


    public void initData(SurVeyOrderBean surVeyOrderBean) {
        if (null != surVeyOrderBean) {
            this.surVeyOrderBean = surVeyOrderBean;
            SurveyFeedbackContentBean surveyFeedbackContentBean = surVeyOrderBean.getSurveyFeedbackContentBean();
            if (null != surveyFeedbackContentBean) {
                List<SurveyFeedBackForBaseInfoBean> prepareList = surveyFeedbackContentBean.getPrepareList();
                if (null != prepareList) {
                    this.prepareDataList.clear();
                    this.prepareDataList.addAll(prepareList);
                    prepareAdapter.setData(prepareDataList);
                    prepareAdapter.notifyDataSetChanged();
                }

                List<SurveyFeedBackForBaseInfoBean> baseInfoList = surveyFeedbackContentBean.getBaseInfoList();
                if (null != baseInfoList) {
                    this.baseinfoDataList.clear();
                    this.baseinfoDataList.addAll(baseInfoList);
                    baseinfoAdapter.setData(baseinfoDataList);
                    baseinfoAdapter.notifyDataSetChanged();
                }

                List<SurveyFeedBackForSizeBean> sizeList = surveyFeedbackContentBean.getSizeList();
                if (null != sizeList) {
                    this.sizeDataList.clear();
                    this.sizeDataList.addAll(sizeList);
                    sizeAdapter.setData(sizeDataList);
                    sizeAdapter.notifyDataSetChanged();
                }

            }

            CommonEnum.CompleteStatus completeStatus = surVeyOrderBean.getCompleteStatus();
            if (null != completeStatus) {
                if (completeStatus == CommonEnum.CompleteStatus.WORKING) {
                    btnClicable(true);
                } else if (completeStatus == CommonEnum.CompleteStatus.WORKER_COMPLETE) {
                    btnClicable(false);

                } else if (completeStatus == CommonEnum.CompleteStatus.ADMIN_COMPLETE) {
                    btnClicable(false);
                } else if (completeStatus == CommonEnum.CompleteStatus.ADMIN_COMPLETE) {
                    btnClicable(false);
                }
            }
        }

    }

    private void btnClicable(boolean bool) {
        isClick = bool;
        if (isClick) {
            btn_submit.setClickable(true);
            btn_submit.setBackgroundResource(R.mipmap.btn_shor_bg_blue);
        } else {
            btn_submit.setClickable(false);
            btn_submit.setBackgroundResource(R.mipmap.btn_bg_short_gray);
        }
    }

    private void submit() {
        if (null != surveyFeedbackAddPresenter) {
            surveyFeedbackAddPresenter.surveyFeedbackAdd();
        }
    }

    @Override
    public void onClick(View view) {
        Intent mIntent;
        switch (view.getId()) {
            // 尺寸记录
            case R.id.img_add_feedback_size:
                if (null != surVeyOrderBean) {
                    // mIntent = new Intent(getActivity(), SurveyAddSizeFeedBackActivity.class);
                    mIntent = new Intent(getActivity(), SurveyAddSizeFeedBackActivity1.class);
                    mIntent.putExtra(ConstantUtil.KEY_1, surVeyOrderBean);
                    startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);
                }
                break;
            // 基础说明
            case R.id.img_add_feedback_baseInfo:
                if (null != surVeyOrderBean) {
                    mIntent = new Intent(getActivity(), SurveyAddBaseFeedBackActivity.class);
                    mIntent.putExtra(ConstantUtil.KEY_1, surVeyOrderBean);
                    startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_2);
                }
                break;
            // 准备工作
            case R.id.img_add_feedback_prepare:
                if (null != surVeyOrderBean) {
                    mIntent = new Intent(getActivity(), SurveyAddPrepareWorkFeedBackActivity.class);
                    mIntent.putExtra(ConstantUtil.KEY_1, surVeyOrderBean);
                    startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_2);
                }
                break;
            // 提交
            case R.id.btn_submit:
                submit();
                break;


        }
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case ConstantUtil.START_ACTIVITY_KEY_1: //尺寸反馈
                if (null != data) {
                    SurveyFeedBackForSizeBean surveyFeedBackForSizeBean = (SurveyFeedBackForSizeBean) data.getSerializableExtra(ConstantUtil.KEY_1);
                    sizeDataList.add(surveyFeedBackForSizeBean);
                    sizeAdapter.setData(sizeDataList);
                    sizeAdapter.notifyDataSetChanged();
                }
                break;
            case ConstantUtil.START_ACTIVITY_KEY_2: //基础或准备反馈
                if (null != data) {
                    SurveyFeedBackForBaseInfoBean surveyFeedBackForBaseInfoBean = (SurveyFeedBackForBaseInfoBean) data.getSerializableExtra(ConstantUtil.KEY_1);
                    if (null != surveyFeedBackForBaseInfoBean) {

                        CommonEnum.SurveyFunctionType surveyFunctionType = surveyFeedBackForBaseInfoBean.getSurveyFunctionType();
                        if (null != surveyFunctionType) {
                            if (surveyFunctionType == CommonEnum.SurveyFunctionType.base) {
                                baseinfoDataList.add(surveyFeedBackForBaseInfoBean);
                                baseinfoAdapter.setData(baseinfoDataList);
                                baseinfoAdapter.notifyDataSetChanged();
                            } else if (surveyFunctionType == CommonEnum.SurveyFunctionType.preparework) {
                                prepareDataList.add(surveyFeedBackForBaseInfoBean);
                                prepareAdapter.setData(prepareDataList);
                                prepareAdapter.notifyDataSetChanged();
                            }
                        }

                    }

                }
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public List<SurveyFeedBackForBaseInfoBean> getBaseInfoList() {
        return baseinfoDataList;
    }

    @Override
    public List<SurveyFeedBackForBaseInfoBean> getPrepareList() {
        return prepareDataList;
    }

    @Override
    public List<SurveyFeedBackForSizeBean> getSizeList() {
        return sizeDataList;
    }

    @Override
    public String getOrderId() {
        if (null != surVeyOrderBean)
            return surVeyOrderBean.getId();

        return "";
    }

    @Override
    public void surveyFeedbackAddSuccess() {
        Toast.makeText(getActivity(), "勘察反馈成功！", Toast.LENGTH_SHORT).show();
        btnClicable(false);
    }

    @Override
    public void surveyFeedbackAddFailed(String desc) {
        Toast.makeText(getActivity(), desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
